/**
 * Created with CLion.
 * Description: 
 * User: loong
 * Date: 2025/4/22
 * Time: 22:49
 */

#include "SceneMain.h"

#include "Enemy.h"
#include "Player.h"
#include "Spawner.h"
#include "screen/UiMouse.h"
#include "world/Effect.h"
#include "world/Spell.h"
void SceneMain::Init()
{
    Scene::Init();
    SDL_HideCursor();

    world_size_ = game_.GetScreenSize() * 3.0f;
    camera_position_ = world_size_ / 2.0f - game_.GetScreenSize() / 2.0f;

    player_ = new Player();
    player_->Init();
    player_->SetPosition(world_size_ / 2.0f);

    AddChild(player_);

    spawner_ = new Spawner();
    spawner_->Init();
    spawner_->SetTarget(player_);
    AddChild(spawner_);

    ui_mouse_ = UIMouse::AddUIMouseChild(this, "../assets/UI/29.png", "../assets/UI/30.png", 1.0f, Anchor::CENTER);

}

void SceneMain::Update(float _dt)
{
    Scene::Update(_dt);
}

void SceneMain::Render()
{
    RenderBackground();
    Scene::Render();
}

void SceneMain::Clean()
{
    Scene::Clean();
}

void SceneMain::HandleEvent(SDL_Event & _event)
{
    Scene::HandleEvent(_event);
    if (_event.type == SDL_EVENT_MOUSE_BUTTON_DOWN)
    {
        if (_event.button.button == SDL_BUTTON_LEFT && player_->IsAlive())
        {
            auto pos = game_.MousePosition() + camera_position_;
            Spell::AddSpellChild(this, "../assets/effect/Thunderstrike w blur.png", pos, 120.0f, 3.0f, Anchor::CENTER);
        }
    }
}

void SceneMain::RenderBackground()
{
    auto start = -camera_position_;
    auto end   = world_size_ - camera_position_;
    game_.DrawGrid(start, end, 80.0f, {0.5, 0.5, 0.5, 1.0});
    game_.DrawBoundary(start, end, 5.0f, {1.0,1.0,1.0, 1.0});
}